<script type="text/ecmascript-6">
    /**
     * Created by wcz on 2016/11/22.
     */
    import b from '../bus';

    export default {
        data () {
            return {
                page: 'submit',
                userInfo: {}
            }
        },
        beforeRouteEnter (to, from, next) {
            axios.get(host + '/downstreams/heartbeat', {
                withCredentials: true,
                headers: {
                    'Content-Type': 'application/json',
                    'Cache-control': 'no-cache'
                },
            }).then(res => {
                if (res.data.success) {
                    next();
                } else {
                    next('/login')

                }
            })
        },
        created () {
            this.get('/downstreams/heartbeat', {}, {slient: true}).then(res => {
                if (!res.success) {
                    this.$router.push('/login')
                }
            })

            b.$on('getInfo', () => {
                this.getInfo();
            })
        },
        mounted () {
            this.getInfo();
        },
        methods: {
            getInfo () {
                this.get('/downstreams/info', {}, {errmsg: '获取用户信息失败', slient: true})
                    .then(res => {
                        if (res.success) {
                            this.userInfo = res.data.info;
                            b.$emit('gotcha')
                        }
                    })
            }
        },
        watch: {
            page (val) {
                this.$router.push(`/home/${val}`)
            }
        }
    };
</script>

<template>
    <div class="page-wrapper" layout="column top-center">
        <transition mode="out-in" name="fade">
            <router-view class="page-container" :userInfo="userInfo">

            </router-view>
        </transition>

        <mt-tabbar v-model="page">
            <mt-tab-item id="submit">
                <i slot="icon" class="iconfont">&#xe6c1;</i>
            </mt-tab-item>
            <mt-tab-item id="list">
                <i slot="icon" class="iconfont">&#xe61d;</i>
            </mt-tab-item>
            <mt-tab-item id="profile">
                <i slot="icon" class="iconfont">&#xe6ab;</i>
            </mt-tab-item>
            <mt-tab-item id="money" v-if="userInfo.role == 0">
                <i slot="icon" class="iconfont">&#xe60f;</i>
            </mt-tab-item>
        </mt-tabbar>
    </div>
</template>